package com.cyz.admin.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyz.admin.service.UsersService;
import com.cyz.common.bean.User;
import com.cyz.common.bean.UserRole;
import com.cyz.common.dto.UserDto;
import com.cyz.common.enums.MyEnums;
import com.cyz.common.mapper.UserMapper;
import com.cyz.common.mapper.UserRoleMapper;
import com.cyz.common.utils.BeanCopyUtil;
import com.cyz.common.utils.R;
import com.cyz.admin.vo.AdminVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UsersServiceImpl extends ServiceImpl<UserMapper, User>
        implements UsersService {

    @Autowired
    private PasswordEncoder passwordEncoder;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private UserRoleMapper userRoleMapper;

    @Override
    public R queryUsers() {
//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //查询所有角色信息
        List<User> list = userMapper.queryUsers();
        //vo处理
        List<AdminVo> adminVos = BeanCopyUtil.copyBeanList(list, AdminVo.class);
        //添加角色id
        adminVos.forEach(item ->{
            item.setRoles(userRoleMapper.getRolesByUserId(item.getId()));
        });
        //返回数据
        return R.success(adminVos);
    }

    @Override
    public R updateUsers(UserDto userDto) {
        //转化dto
        User user = BeanCopyUtil.copyBean(userDto, User.class);
        //加密密码
        if(userDto.getPassword() != null){
            user.setPassword(passwordEncoder.encode(user.getPassword()));
        }
        //获取新角色id数组
        List<Integer> roleIds = userDto.getRoleIds();

        userRoleMapper.removeById(user.getId());
//        adminAdminUserRoleService.removeById(user.getId());
//        LambdaQueryWrapper<UserRole> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        roleIds.forEach(item ->{
//            lambdaQueryWrapper.eq(UserRole::getUserId,userDto.getId());
//            lambdaQueryWrapper.eq(UserRole::getRoleId,item);
//            UserRole one = adminUserRoleService.getOne(lambdaQueryWrapper);
//            if(ObjectUtils.isEmpty(one)){
//            adminAdminUserRoleService.save(new UserRole(userDto.getId(),item));
            userRoleMapper.save(new UserRole(userDto.getId(),item));
//            }else{
//
//            }
        });
//        //保存数据
//        boolean flag = updateById(user);
        boolean flag = userMapper.updateUser(user);
        //错误判断
        if(flag){
            return queryUsers();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R addUser(UserDto userDto) {
        //转化dto
        User user = BeanCopyUtil.copyBean(userDto, User.class);
        //加密密码
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        user.setType(MyEnums.USER_TYPE_ADMIN);
        //保存数据
        boolean flag = userMapper.save(user);
//        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
//        lambdaQueryWrapper.eq(User::getUserName,userDto.getUserName());
//        Integer id = getOne(lambdaQueryWrapper).getId();
        userDto.getRoleIds().forEach(item ->{
//            adminAdminUserRoleService.save(new UserRole(user.getId(),item));
            userRoleMapper.save(new UserRole(user.getId(),item));
        });
        //错误判断
        if(flag){
            return queryUsers();
        }
        return R.error(MyEnums.ADD_FAIL);
    }

    @Override
    public R deleteUsers(Integer[] ids) {
        //删除数据
        boolean flag = userMapper.removeByIds(ids);
        //错误判断
        if (flag){
            return queryUsers();
        }
        return R.error(MyEnums.DELETE_FAIL);
    }

    @Override
    public R updateUserStatus(UserDto userDto) {
        //转化dto
        User user = BeanCopyUtil.copyBean(userDto, User.class);
        //保存数据
//        boolean flag = updateById(user);

        boolean flag = userMapper.updateUserStatus(user);

        //错误判断
        if(flag){
            return queryUsers();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }

    @Override
    public R updateUserPassword(Integer id) {

        User user = userMapper.getUserByid(id);

        user.setPassword(passwordEncoder.encode("123456"));

        boolean flag = userMapper.updateUser(user);
        //错误判断
        if(flag){
            return queryUsers();
        }
        return R.error(MyEnums.UPDATE_FAIL);
    }
}
